import { _decorator, Button, Component, Node, Sprite, SpriteFrame, UIOpacity, Vec3, Animation, CCFloat, CCBoolean } from 'cc';
import { isAllowNextStep } from '../utils/common';
const { ccclass, property } = _decorator;

@ccclass('StudentControl')
export class StudentControl extends Component {
    @property({type: CCFloat})
    public speed: number = 100.0;
    @property({type: CCBoolean})
    public motion_direction;   // 运动方向，true代表向右，false代表向左
    @property({type: Button})
    public active_target_btn: Button = null;    // 激活目标按钮
    @property({type: Button})
    public reset_target_btn: Button = null;     // 重置目标按钮

    private max_distance: number = 100;     // 移动的最大距离
    private motation_distance: number = 0;  // 移动的总距离
    private is_motion: boolean = false;   // 是否开始移动
    private is_initial_pos: boolean = true;       // 是否在初始位置
    private is_zero_motion: boolean = true;       // 是否0次移动

    start() {
    }

    protected onLoad(): void {
        this.active_target_btn.node.on(Button.EventType.CLICK, this.onActiveTargetBtnClick, this);
        this.reset_target_btn.node.on(Button.EventType.CLICK, this.onResetTargetBtnClick, this);
    }

    protected onDestroy(): void {
        if (this.active_target_btn !== null && this.active_target_btn !== undefined && this.active_target_btn.name.length) {
            this.active_target_btn.node.off(Button.EventType.CLICK, this.onActiveTargetBtnClick, this);
        }
        if (this.reset_target_btn !== null && this.reset_target_btn !== undefined && this.reset_target_btn.name.length) {
            this.reset_target_btn.node.off(Button.EventType.CLICK, this.onResetTargetBtnClick, this);
        }
    }

    update(deltaTime: number) {
        this.studentMotion(deltaTime);
    }

    onActiveTargetBtnClick() {
        /**
         * 触发器被点击后：学生精灵就需要向指定方向移动
         * 1. 获取学生精灵。
         * 2. 设置需要移动的最终位置。
         * 3. 操纵学生精灵移动。
        */
        // this.is_motion = true;
        // this.playAnimation();
        if (isAllowNextStep(localStorage.getItem('GameStep'), 'Option')) {
            if (this.is_initial_pos) { // 在初始位置，才能激活行动
                if(this.is_zero_motion) {
                    this.is_motion = true;
                    this.is_initial_pos = false;
                    this.is_zero_motion = false;
                    this.playAnimation();
                } else {
                    this.motion_direction = !this.motion_direction;
                    this.is_motion = !this.is_motion;
                    this.is_initial_pos = !this.is_initial_pos;
                    this.motation_distance = 0;
                    this.playAnimation();
                }
            }
        } else {
            // throw new Error("请根据按钮顺序逐步操作");
        }
    }

    onResetTargetBtnClick() {
        /**
         * 如果位置没有移动，那么便不用复位
        */
        if (isAllowNextStep(localStorage.getItem('GameStep'), 'NextQuestion')) {
            if(!this.is_initial_pos) {  // 不在初始位置，才能激活复位
                this.motion_direction = !this.motion_direction;
                this.is_motion = !this.is_motion;
                this.is_initial_pos = !this.is_initial_pos;
                this.motation_distance = 0;
                // 当回到原位时才能关闭动画
                this.playAnimation();
            }
        } else {
            // throw new Error("请根据按钮顺序逐步操作");
        }
    }

    studentMotion(deltaTime: number) {
        /**
         * 学生运动函数
        */
        if (this.is_motion) {
            const now_pos = this.node.position;
            this.motation_distance += this.speed * (this.motion_direction?1:-1) * deltaTime;
            if (this.motion_direction) {
                if (this.motation_distance <= this.max_distance) {
                    this.node.setPosition(new Vec3(now_pos.x + this.speed * (this.motion_direction?1:-1) * deltaTime, now_pos.y, now_pos.z));
                } else {
                    this.is_motion = false;
                }
            } else {
                if (Math.abs(this.motation_distance) <= this.max_distance) {
                    this.node.setPosition(new Vec3(now_pos.x + this.speed * (this.motion_direction?1:-1) * deltaTime, now_pos.y, now_pos.z));
                } else {
                    this.is_motion = false;
                }
            }
        }    
    }

    playAnimation() {
        /**
         * 切换学生精灵框架
         * 1. 获取学生精灵框架图片。
         * 2. 图片一共有两张，每帧切换一幅图片
        */
        if (this.is_motion) {
            const animation = this.node.getComponent(Animation);
            let animation_name: string = `${this.node.name}Animation`
            animation.getState(animation_name).repeatCount = 2;
            animation.getState(animation_name).speed = 0.5;
            animation.play(animation_name);
        }
    }

    stopAnimation() {
        /**
         * 暂停
        */
        if (this.is_motion) {
            const animation = this.node.getComponent(Animation);
            let animation_name: string = `${this.node.name}Animation`
            // animation.getState(animation_name).repeatCount = 2;
            // animation.getState(animation_name).speed = 0.5;
            // animation.play(animation_name);
            animation.stop();
        }
    }
}


